package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.*;
import com.clkj.xiuzheng.pojo.*;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.clkj.xiuzheng.pojo.common.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class InStockServiceImpl implements InStockService{

    @Autowired
    private InStockMapper inStockMapper;

    @Autowired
    private InStockItemMapper inStockItemMapper;

    @Autowired
    private ProductStockMapper productStockMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private WarehouseMapper  warehouseMapper;


    @Override
    @Transactional(rollbackFor = Exception.class, noRollbackFor = RuntimeException.class)
    public void addInStock(IntoStock intoStock) {
        String dateTime = DateFormatHelper.currentTimes();
        intoStock.setApplyStatus(0);
        intoStock.setIntoDate(dateTime);
        //添加入库
        inStockMapper.addIntoStock(intoStock);
        //添加入库明细
        for (OutStockItem item : intoStock.getList()){
            item.setOutId(intoStock.getIntoId());
            item.setItemDate(dateTime);
            item.setItemFlag(0);
        }
        inStockItemMapper.insertList(intoStock.getList());
    }

    @Override
    public PageInfo getInStock(Integer pageSize, Integer pageNum,Integer companyId) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(IntoStock.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("companyId",companyId);
        example.orderBy("intoId").desc();
        List<IntoStock> intoStocks = inStockMapper.selectByExample(example);

        return  new PageInfo<>(intoStocks);
    }

    @Override
    public PageInfo getInStockItem(Integer pageSize, Integer pageNum, Integer intoId) {
        PageHelper.startPage(pageNum,pageSize);
        List<OutStockItem> outStockItems = inStockItemMapper.selectAllById(intoId);
//        for (OutStockItem outStockItem:outStockItems){
//            //添加商品名称
//            Product product = productMapper.selectProductById(outStockItem.getProductId());
//            outStockItem.setProductName(product.getProductName());
//            //添加商品格式
//          outStockItem.setFormatName(product.getFormatName());
//          //添加商品仓库
//          Warehouse warehouse = warehouseMapper.selectWarehouseById(outStockItem.getWarehouseId());
//         outStockItem.setWarehouseName(warehouse.getWarehouseName());
//        }
        return new PageInfo<>(outStockItems);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void applyInStockItem(Integer intoId) {



        //通过审核后给商品表添加总数
        //查询出所有要添加的商品总数
//        List<OutStockItem> intoStockItems = inStockItemMapper.selectAllById(intoId);
        Example example =  new Example(OutStockItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("outId",intoId).andEqualTo("itemFlag",0);
        List<OutStockItem> intoStockItems = inStockItemMapper.selectByExample(example);
        for (OutStockItem item : intoStockItems) {

            example = new Example(ProductStock.class);
            criteria = example.createCriteria();
            criteria.andEqualTo("productId", item.getProductId()).andEqualTo("barCode", item.getBarCode())
                    .andEqualTo("warehouseId", item.getWarehouseId());
            ProductStock stock = productStockMapper.selectOneByExample(example);
            if (stock == null) {
                stock = new ProductStock();
                stock.setProductId(item.getProductId());
                stock.setBarCode(item.getBarCode());
                stock.setProductDate(item.getProductSate());
                stock.setValidity(item.getValidity());
                stock.setWarehouseId(item.getWarehouseId());
                stock.setCompanyId(item.getCompanyId());
                stock.setProductNum(item.getProductNum());
                stock.setFactoryId(item.getFactoryId());
                stock.setProductUnit(item.getItemUnit());
                stock.setSalePrice(item.getProductPrice());
                productStockMapper.insertSelective(stock);
            } else {
                stock.setProductNum(stock.getProductNum() + item.getProductNum());
                productStockMapper.updateByPrimaryKeySelective(stock);
            }

        }
        //通过审核
//        inStockItemMapper.updateApplyStatus(intoId);
        IntoStock intoStock = new IntoStock();
        intoStock.setApplyStatus(1);
        intoStock.setIntoId(intoId);
        inStockMapper.updateByPrimaryKeySelective(intoStock);


//        for (OutStockItem item : intoStockItems){
//            //更新商品详情总数
//            //先看有没有这个批号的商品
//            ProductStock productStock =productStockMapper.selectProductItemNumByProductIdAndBarCode(item.getProductId(),item.getBarCode());
//            if (productStock==null){
//                //暂时没有该批号商品 添加该批号商品
//                //根据商品ID和商品批号查询商品信息
//                Product product = inStockItemMapper.selectProductByProductIdAndBarCode(item.getProductId());
//                //生成该批号对象
//                ProductStock productStock1 = new ProductStock();
//                productStock1.setProductId(product.getProductId());
//                productStock1.setBarCode(item.getBarCode());
//                productStock1.setProductDate(product.getProductDate());
//                productStock1.setValidity(product.getProductValidity());
//                productStock1.setWarehouseId(item.getWarehouseId());
//                productStock1.setCompanyId(item.getCompanyId());
//                productStock1.setProductNum(item.getProductNum());
//                productStock1.setFactoryId(item.getFactoryId());
//                productStock1.setProductUnit(item.getItemUnit());
//                productStock1.setSalePrice(item.getProductPrice());
//                productStockMapper.insertSelective(productStock1);
//            }
//            //有该批号商品 直接更新总数
//            productStockMapper.updateProductNumByProductIdAndBarCode(item.getProductId(),item.getBarCode(),item.getProductNum());
//        }
    }


}
